package database.service;

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;

import database.auxiliary.LogGenerator;

import po.MemberPO;

public class MemberDataServiceSerializableImpl implements MemberDataService {

	private ArrayList<MemberPO> memberList;

	public MemberDataServiceSerializableImpl() {
		memberList = new ArrayList<MemberPO>();
	}

	@Override
	public void load() {
		try {
			File file = new File("data/member.ser");
			if (!file.exists()) {
				file.createNewFile();
			}
			ObjectInputStream oiStream = new ObjectInputStream(
					new FileInputStream(file));
			MemberPO memberPO;
			while ((memberPO = (MemberPO) oiStream.readObject()) != null) {
				memberList.add(memberPO);
			}
			LogGenerator.log("Member data load succeed, length: " + memberList.size());
		} catch (EOFException e) {
			save();
			load();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void save() {
		try {
			FileOutputStream fos = new FileOutputStream("data/member.ser");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			for (int i = 0; i < memberList.size(); i++) {
				oos.writeObject(memberList.get(i));
			}
			oos.writeObject(null);
			oos.close();
			LogGenerator.log("Member data save succeed");
		} catch (FileNotFoundException e) {
			System.out.println("File found failed!");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public int insert(MemberPO member) {
		memberList.add(member);
		LogGenerator.log("Member insert: " + member.toString());
		return member.getId();
	}

	@Override
	public void delete(MemberPO member) {
		for (int i = 0; i < memberList.size(); i++) {
			if (memberList.get(i).getId() == member.getId()) {
				memberList.get(i).setId(-memberList.get(i).getId());
				LogGenerator.log("Member delete: " + member);
			}
		}
	}

	@Override
	public void update(MemberPO member) {
		for (int i = 0; i < memberList.size(); i++) {
			if (member.getId() == memberList.get(i).getId()) {
				memberList.set(i, member);
				LogGenerator.log("Member update: " + member);
			}
		}
	}

	@Override
	public MemberPO find(int id) {
		for (int i = 0; i < memberList.size(); i++) {
			if (id == memberList.get(i).getId()) {
				LogGenerator.log("Member find: " + memberList.get(i));
				return new MemberPO(memberList.get(i));
			}
		}
		return null;
	}

	@Override
	public Iterator<MemberPO> getIterator() {
		LogGenerator.log("Member return itr, length = " + memberList.size());
		return memberList.iterator();
	}
	
//	public static void main(String args[]) {
//		MemberDataService memberDataService = new MemberDataServiceSerializableImpl();
//		memberDataService.load();
//		memberDataService.insert(new MemberPO(1, "satansin", "123456", UserIdentity.CUSTOMER));
//		memberDataService.insert(new MemberPO(2, "liuhao1", "123456", UserIdentity.CUSTOMER));
//		memberDataService.insert(new MemberPO(3, "liuhao2", "123456", UserIdentity.CUSTOMER));
//		memberDataService.insert(new MemberPO(4, "liuhao3", "123456", UserIdentity.CUSTOMER));
//		memberDataService.insert(new MemberPO(5, "liuhao4", "123456", UserIdentity.CUSTOMER));
//		memberDataService.insert(new MemberPO(6, "liuhao5", "123456", UserIdentity.CUSTOMER));
//		memberDataService.insert(new MemberPO(7, "liuhao6", "123456", UserIdentity.CUSTOMER));
//		memberDataService.save();
//	}

}
